{layout name="main"/}
<style>
    .modal-backdrop,
    .fade,
    .in {
        display: none;
    }
</style>
<div class="wrapper wrapper-content animated fadeInUp">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox">

                <div class="ibox-title">
                    <h5>数据库备份</h5>

                </div>

                <div class="ibox-content">
                    <div class="row m-b-sm m-t-sm">

                        <div class="col-sm-12 text-right">
                            <button class="btn btn-primary btn-xl" onclick="backup()"><i class="fa fa-database"></i> 备份数据库 </button>


                        </div>
                    </div>
                    <div class="project-list">
                        <table class="table table-striped table-bordered table-hover dataTables-example dataTable" id="DataTables_Table_0" aria-describedby="DataTables_Table_0_info">
                            <thead>
                                <tr role="row">
                                    <th style="width: 1%">选择</th>
                                    <th style="width: 12%;">名称</th>
                                    <th style="width:20%;">备份时间</th>
                                    <th style="width:5%;">大小</th>
                                    <th style="width:10%;">操作</th>
                                </tr>
                            </thead>

                            <!--                            <tbody><tr class="no-records-found"><td colspan="8">没有找到匹配的记录</td></tr></tbody>-->

                            <tbody>
                              {:table_empty_list($list,'暂无数据!',5)}
                                {volist name="list" id="back"}
                                <tr class="gradeA odd">
                                    <td>
                                        <input type="checkbox" class="i-checks" value="{$back.name}" name="ids[]">
                                    </td>

                                    <td class=" ">
                                        {if $back.name eq "20160922180022.sql"}
                                        原始数据库
                                        {else}
                                        {$back.name}
                                        {/if}

                                    </td>

                                    <td class=" ">
                                        {$back.time}
                                    </td>

                                    <td class=" ">
                                        {$back.size}
                                    </td>

                                    <td class="center">
                                        {if $back.name neq "20160922180022.sql"}
                                        <button class="btn btn-danger btn-xs" onclick="del('{$back.name}')"><i class="fa fa-trash"></i> 删除 </button>
                                        {/if}

                                        <a href="{:url('admin/Backup/down',[],'')}/backname/{$back.name}" class="btn btn-primary btn-xs"><i class="fa fa-download"></i>下载</a>

                                        <button class="btn btn-warning btn-xs" onclick="restore('{$back.name}')">还原数据库</button>


                                    </td>
                                </tr>
                                {/volist}

                            </tbody>

                        </table>
                        <div class="row m-b">
                            <div class="col-sm-6 m-t-sm">
                                <button class="btn btn-primary btn-xs" id="selectAll"><i class="fa fa-check"></i> 全选 </button>
                              <button class="btn btn-warning btn-xs" id="deleteAll"><i class="fa fa-trash"></i> 批量删除 </button>
                            </div>
                        </div>

                        <div class="row" style=" display: none">
                            <div class="col-sm-6">
                                <div class="dataTables_info" id="DataTables_Table_0_info" role="alert" aria-live="polite" aria-relevant="all">
                                   </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="dataTables_paginate paging_simple_numbers" id="DataTables_Table_0_paginate">


                                </div>

                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>



<script>
    $(document).ready(function() {
        $(".i-checks").iCheck({
            checkboxClass: "icheckbox_square-green",
            radioClass: "iradio_square-green",
        });
        $("#selectAll").on('click', function(event) { //如果是选中，点击后则为不选中
            $(".i-checks").iCheck('check');
        });
        $("#deleteAll").click(function() {
            swal({
                title: "您确定要删除这些备份吗",
                text: "删除后将无法恢复，请谨慎操作！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "删除",
                closeOnConfirm: false,
                cancelButtonText: "取消",
            }, function() {
                var postdata = '';
                if ($("input[type='checkbox']:checked").length == 0) {
                    swal("错误", "请至少选择1个备份。", "error");
                    return;
                }
                $("input[type='checkbox']:checked").each(function() {

                    postdata = postdata == '' ? 'ids[]=' + $(this).val() : postdata + '&ids[]=' + $(this).val();
                });
                if (postdata != '') {
                    post({
                        url: "{:url('admin/Backup/del_multi')}",
                        data: postdata,
                        success: function(res) {
                            swal({
                                title: 'success',
                                text: res.message,
                                type: "success"
                            }, function() {
                                reload();
                            });
                        },
                        error: function(error) {
                            swal("错误", JSON.parse(error.responseText).message, "error");
                        }
                    });
                }
            });
        });

    });

    function del(backname) {
        if (confirm("您是否确认删除?")) {
            post({
                url: "{:url('admin/Backup/del',[],'')}/backname/" + backname,
                success: function(res) {
                    swal({
                        title: 'success',
                        text: res.message,
                        type: "success"
                    }, function() {
                        reload();
                    });
                },
                error: function(error) {
                    swal("错误", JSON.parse(error.responseText).message, "error");
                }
            });
        }

    }

    function restore(name) {

        if (confirm("您是否恢复此备份？")) {
            post({
                url: "{:url('admin/Backup/restore',[],'')}/backname/" + name,
                success: function(res) {
                    swal({
                        title: 'success',
                        text: res.message,
                        type: "success"
                    }, function() {
                        reload();
                    });
                },
                error: function(error) {
                    swal("错误", JSON.parse(error.responseText).message, "error");
                }
            });
        }
    }

    function backup() {
        post({
            url: "{:url('admin/Backup/backup')}",
            success: function(res) {
                swal({
                    title: 'success',
                    text: res.message,
                    type: "success"
                }, function() {
                    reload();
                });
            },
            error: function(error) {
                swal("错误", JSON.parse(error.responseText).message, "error");
            }
        });
    }
</script>
